package com.loc.security;

import java.util.Collection;
import java.util.Iterator;


import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

public class MyAccssDecisionManager implements AccessDecisionManager{

	public void decide(Authentication authentication, Object arg1,
			Collection<ConfigAttribute> attributes) throws AccessDeniedException,
			InsufficientAuthenticationException {
		if(attributes==null){
			return;
		}
		Iterator<ConfigAttribute> iterator = attributes.iterator();
		while(iterator.hasNext()){
			ConfigAttribute configAttribute = iterator.next();
			String needPermission = configAttribute.getAttribute();
			System.out.println("needPermission is "+needPermission);
			for(GrantedAuthority ga:authentication.getAuthorities()){
				if(needPermission.equals(ga.getAuthority())){
					return;
				}
			}
		}
		throw new AccessDeniedException("没有权限!");
	}

	public boolean supports(Class<?> arg0) {
		return true;
	}

	public boolean supports(ConfigAttribute arg0) {
		return true;
	}

}
